package net.lab1024.sa.base.module.support.wxsm.service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.amazonaws.util.DateUtils;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.wxsm.dao.WxsmDao;
import net.lab1024.sa.base.module.support.wxsm.domain.entity.WxsmEntity;
import net.lab1024.sa.base.module.support.wxsm.domain.form.WxsmAddForm;
import net.lab1024.sa.base.module.support.wxsm.domain.form.WxsmQueryForm;
import net.lab1024.sa.base.module.support.wxsm.domain.form.WxsmUpdateForm;
import net.lab1024.sa.base.module.support.wxsm.domain.vo.WxsmVO;
import net.lab1024.sa.base.utils.DateUtilsLocal;
import net.lab1024.sa.base.utils.TextConversionUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 消息推送 Service
 *
 * @Author chenyuqiang
 * @Date 2025-09-06 23:11:23
 * @Copyright 1
 */

@Service
public class WxsmService {

    @Resource
    private WxsmDao wxsmDao;

    /**
     * 分页查询
     */
    public PageResult<WxsmVO> queryPage(WxsmQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<WxsmVO> list = wxsmDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(WxsmAddForm addForm) {
        WxsmEntity wxsmEntity = SmartBeanUtil.copy(addForm, WxsmEntity.class);
        wxsmEntity.setCreateTime(DateUtilsLocal.formatLocalDateTime(LocalDateTime.now()));
        wxsmEntity.setUpdateTime(DateUtilsLocal.formatLocalDateTime(LocalDateTime.now()));
        wxsmEntity.setCreateUser(SmartRequestUtil.getRequestUser().getUserName());
        String epl = wxsmEntity.getEpl();
        String cnversion = TextConversionUtils.cnversion(epl);
        wxsmEntity.setEpl(cnversion);
        wxsmDao.insert(wxsmEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(WxsmUpdateForm updateForm) {
        WxsmEntity wxsmEntity = SmartBeanUtil.copy(updateForm, WxsmEntity.class);
        wxsmEntity.setUpdateTime(DateUtilsLocal.formatLocalDateTime(LocalDateTime.now()));
        wxsmEntity.setUpdateUser(SmartRequestUtil.getRequestUser().getUserName());
        String epl = wxsmEntity.getEpl();
        String cnversion = TextConversionUtils.cnversion(epl);
        wxsmEntity.setEpl(cnversion);
        wxsmDao.updateById(wxsmEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        wxsmDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        wxsmDao.deleteById(id);
        return ResponseDTO.ok();
    }
}
